package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import util.JDBCUtil;
import domain.Actor;
import ex.DatabaseException;

public class ActorDAO {
	private Connection conn;
	private PreparedStatement stmt;
	private ResultSet rs;
	
	private static final String CREATE_ACTOR="insert into actorPysFea (height,weight,hairColor,eyeColor,skinColor,uid) values (?,?, ?, ?, ?, ?) ";
	private static final String EDIT_ACTOR="update actorPysFea set height = ?, weight = ?, hairColor = ?, eyeColor = ?, skinColor = ? where uid = ?";
	private static final String SELECT_ACTOR_BY_UID="select * from actorPysFea where uid = ?";
	private static final String DELETE_ACTOR="delete from actorPysFea where uid = ?";

	//Selection of the Actor Information using the User ID in the Actor table
	public Actor selectActorByUid(long uid) throws DatabaseException
	{
		conn = JDBCUtil.getConnection();
		
		Actor actor = null;
		try {
			stmt = conn.prepareStatement(SELECT_ACTOR_BY_UID);
			stmt.setLong(1, uid);
			
			rs = stmt.executeQuery();
			if (rs.next()) {
				String hairColor = rs.getString("hairColor");
				String eyeColor = rs.getString("eyeColor");
				String skinColor = rs.getString("skinColor");
				int height = rs.getInt("height");
				int weight = rs.getInt("weight");
				
				actor =new Actor(uid, hairColor, eyeColor, skinColor,height, weight );
			}
			stmt.close();
			JDBCUtil.closeConnection(conn);
		} 
		catch (SQLException e) {
			throw new DatabaseException("Problem with retrieving user information! "	+ e.getMessage());
		}
		return actor;
	}
	
	//Insertion of a new Actor to the database
	public void insertActor(Actor actor) throws DatabaseException {
		conn = JDBCUtil.getConnection();
		
		try {
			stmt = conn.prepareStatement(CREATE_ACTOR);
			stmt.setInt(1, actor.getHeight());
			stmt.setInt(2, actor.getWeight());
			stmt.setString(3, actor.getHairColor());
			stmt.setString(4, actor.getEyeColor());
			stmt.setString(5, actor.getSkinColor());			
			stmt.setLong(6, actor.getUid());
			
			stmt.execute();			
			stmt.close();
			JDBCUtil.closeConnection(conn);
		} 
		catch (SQLException e) {
			throw new DatabaseException("Problem with inserting user information! "	+ e.getMessage());
		}
	}
	
	//Editing the Actor information
	public void editActor(Actor actor) throws DatabaseException {
		conn = JDBCUtil.getConnection();
		
		try {
			stmt = conn.prepareStatement(EDIT_ACTOR);
			stmt.setInt(1, actor.getHeight());
			stmt.setInt(2, actor.getWeight());
			stmt.setString(3, actor.getHairColor());
			stmt.setString(4, actor.getEyeColor());
			stmt.setString(5, actor.getSkinColor());			
			stmt.setLong(6, actor.getUid());

			stmt.execute();			
			stmt.close();
			JDBCUtil.closeConnection(conn);
		} 
		catch (SQLException e) {
			throw new DatabaseException("Problem with editing user information! "	+ e.getMessage());
		}
	}
	
	public void deleteActor(Actor actor) throws DatabaseException {
		conn = JDBCUtil.getConnection();
		
		try {
			stmt = conn.prepareStatement(DELETE_ACTOR);
			stmt.setLong(1, actor.getUid());
			stmt.execute();			
			stmt.close();
			JDBCUtil.closeConnection(conn);
		} 
		catch (SQLException e) {
			throw new DatabaseException("Problem with deleting actor! "	+ e.getMessage());
		}
	}
}
